package com.itboy.mall.pms.spuimages.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.spuimages.bo.MallSpuImagesAddBo;
import com.itboy.mall.pms.spuimages.bo.MallSpuImagesQueryBo;
import com.itboy.mall.pms.spuimages.bo.MallSpuImagesEditBo;
import com.itboy.mall.pms.spuimages.domain.MallSpuImages;
import com.itboy.mall.pms.spuimages.mapper.MallSpuImagesMapper;
import com.itboy.mall.pms.spuimages.vo.MallSpuImagesVo;
import com.itboy.mall.pms.spuimages.service.IMallSpuImagesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * spu图片Service业务层处理
 *
 * @author itboy
 * @date 2021-08-08
 */
@Service
public class MallSpuImagesServiceImpl extends ServicePlusImpl<MallSpuImagesMapper, MallSpuImages> implements IMallSpuImagesService {

    @Override
    public MallSpuImagesVo queryById(Long id){
        return getVoById(id, MallSpuImagesVo.class);
    }

    @Override
    public TableDataInfo<MallSpuImagesVo> queryPageList(MallSpuImagesQueryBo bo) {
        PagePlus<MallSpuImages, MallSpuImagesVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallSpuImagesVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallSpuImagesVo> queryList(MallSpuImagesQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallSpuImagesVo.class);
    }

    private LambdaQueryWrapper<MallSpuImages> buildQueryWrapper(MallSpuImagesQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallSpuImages> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSpuId() != null, MallSpuImages::getSpuId, bo.getSpuId());
        lqw.like(StrUtil.isNotBlank(bo.getImgName()), MallSpuImages::getImgName, bo.getImgName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallSpuImagesAddBo bo) {
        MallSpuImages add = BeanUtil.toBean(bo, MallSpuImages.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallSpuImagesEditBo bo) {
        MallSpuImages update = BeanUtil.toBean(bo, MallSpuImages.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallSpuImages entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
